#include "WFloat.hpp"
#include <iostream>
#include <chrono>

WFloat pi(int iter, int accuracy = -1)
{
    if (accuracy != -1)
        WFloat::setAccuracy(accuracy);

    WFloat pi = 0;
    WFloat sign = 1;
    WFloat denom = 1;
    WFloat term = 1;
    int i = 0;
    while (term != 0)
    {
        pi += term;
        sign *= -1;
        denom += 2;
        term = sign / denom;
        ++i;
        if (i == iter)
            break;
    }
    return pi * 4;
}

int main()
{
    std::cout << "This is a program to calculate pi using the Leibniz formula.\n"
                 "Press Ctrl+C to exit.\n\n";

    while (true)
    {
        std::cout << "Enter the number of iterations: ";
        int iter;
        std::cin >> iter;
        std::cout << "\nEnter the accuracy of float number (default 100): ";
        int accuracy;
        std::cin >> accuracy;

        auto start = std::chrono::high_resolution_clock::now();
        std::cout << "\nThe value of pi is: " << pi(iter, accuracy) << "\n\n";
        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> elapsed = end - start;
        std::cout << "Elapsed time: " << elapsed.count() << "s\n\n";

        std::cout << "----------------------------------------\n\n";
    }

    return 0;
}